/**
 * Created by Administrator on 2017/3/5.
 */
(function($){

    /*banner切换*/
    var banner={
        init:function(){
            this.picList=$(".scroll_pic li");
            this.smallBtnWrap=$(".small_btn");
            this.prevBtn=$(".banner_prev");
            this.nextBtn=$(".banner_next");
            this.iLen=this.picList.length;
            this.str="";
            this.n=0;
            this.timer=null;
            this.moveTime=600;
            this.delayTime=5000;
            this.picChange();
        },
        picChange:function(){
            if(this.picList.length>1){
                this.addSmallBtn();
                this.prevEvent();
                this.nextEvent();
                this.smallBtnEvent();
                this.setTime();
                this.clearTime();
            }
        },
        addSmallBtn:function(){
            var _this=this;
            this.picList.each(function(index){
                $(this).css("z-index",10-index);
                _this.str+='<a href="javascript:void(0);"></a>';
            });
            this.smallBtnWrap.append(_this.str);
            this.smallBtn=this.smallBtnWrap.find("a");
            this.smallBtn.first().addClass('active');
        },
        setTime:function(){
            var _this=this;
            this.timer=setInterval(function(){
                _this.n++;
                if(_this.n>_this.iLen-1){
                    _this.n=0;
                }
                _this.common();
            },this.delayTime);
        },
        clearTime:function(){
            this.baseHover(this.smallBtn,this);
            this.baseHover(this.prevBtn,this);
            this.baseHover(this.nextBtn,this);
        },
        baseHover:function(obj,that){
            obj.hover(function(){
                clearInterval(that.timer);
            },function(){
                that.setTime();
            });
        },
        prevEvent:function(){
            var _this=this;
            this.prevBtn.on("click",function(){
                _this.n--;
                if(_this.n<0){
                    _this.n=_this.iLen-1;
                }
                _this.common();
            });
        },
        nextEvent:function(){
            var _this=this;
            this.nextBtn.on("click",function(){
                _this.n++;
                if(_this.n>_this.iLen-1){
                    _this.n=0;
                }
                _this.common();
            });
        },
        smallBtnEvent:function(){
            var _this=this;
            this.smallBtn.each(function(index){
                $(this).on("mouseover",function(){
                    _this.n=index;
                    _this.common();
                });
            });
        },
        common:function(){
            var _this=this;
            var list=this.picList.eq(this.n).siblings();
            this.smallBtn.eq(this.n).addClass("active").siblings().removeClass("active");
            this.picList.eq(this.n).css("display","block");
            this.picList.eq(this.n).stop().animate({"opacity":1},this.moveTime);
            this.picList.eq(this.n).siblings().stop().animate({"opacity":0},this.moveTime,function(){
                list.css("display","none");
            });
        }
    };
    banner.init();

    /*师资队伍*/
    function Teacher(opt){
        if(!opt) return;

        this.$bigPicWrap=opt.bigPicWrap;
        this.$wrap=this.$bigPicWrap.closest(".i_teacher");
        this.$List=this.$bigPicWrap.find("li");
        this.$bigPic=this.$bigPicWrap.find("img");
        this.$smallPicWrap=opt.smallPicWrap;
        this.$scrollPic=this.$smallPicWrap.find(".change_b");
        this.$prev=this.$smallPicWrap.find(".prev");
        this.$next=this.$smallPicWrap.find(".next");
        this.iLen=this.$List.length;
        /*this.$List.hide().first().show();*/
        this.maxLen=Math.ceil(this.iLen/6);
        this.n=0;
        this.m=0;
        this.iW=0;
        this.timer=null;
        this.init();
    }
    Teacher.prototype={
        constructor:Teacher,
        init:function(){
            this.addSmallPic();     //添加小图片
            this.smallBtnEvent();  //小图片点击事件
            if(this.maxLen>1){
                this.prevEvent();
                this.nextEvent();
            }
            this.setTime();    //定时器  自动滚动
            this.wrapHover();  //移入清除定时器

        },
        addSmallPic:function(){
            var _self=this,
                str="";

            this.$bigPic.each(function(){
                str+='<a href="javascript:void(0);"><span><img src="'+$(this).attr("src")+'" alt=""></span></a>';
            });
            this.$scrollPic.html(str);
            this.$smallPicA=this.$scrollPic.find("a");
            this.iW=this.$smallPicA.first().outerWidth(true);
            this.$smallPicA.first().addClass("active");

            this.$scrollPic.width(this.iLen*this.iW);
        },
        smallBtnEvent:function(){
            var _self=this;
            this.$smallPicA.each(function(index){
               $(this).on("click",function(){
                   $(this).addClass("active").siblings().removeClass("active");
                   _self.m=index;
                   _self.$bigPicWrap.stop(true,true).animate({"left":- _self.m*_self.$List.first().outerWidth(true)},800,"easeInOutQuint");
                   /*_self.$List.fadeOut(500).eq(index).fadeIn(500);*/
               })
            });
        },
        prevEvent:function(){
            var _self=this;
            this.$prev.on("click",function(){
                _self.n--;
                if(_self.n<0){
                    _self.n=0;
                }
                _self.$scrollPic.stop(true,true).animate({"left":-(_self.n)*_self.$scrollPic.parent().width()},400);
            });
        },
        nextEvent:function(){
            var _self=this;
            this.$next.on("click",function(){
                _self.n++;
                if(_self.n>=_self.maxLen){
                    _self.n=_self.maxLen-1;
                }
                _self.$scrollPic.stop(true,true).animate({"left":-(_self.n)*_self.$scrollPic.parent().width()},400);
            });
        },
        setTime:function(){
            var _self=this;
            this.timer=setInterval(function(){
                _self.m++;
                if(_self.m>=_self.iLen){
                    _self.m=0;
                }
                _self.$smallPicA.eq(_self.m).addClass("active").siblings().removeClass("active");
               /* _self.$List.fadeOut(500).eq(_self.m).fadeIn(500);*/
                _self.$bigPicWrap.stop(true,true).animate({"left":- _self.m*_self.$List.first().outerWidth(true)},800,"easeInOutQuint");
                _self.$scrollPic.stop(true,true).animate({"left":-(Math.floor(_self.m/6))*_self.$scrollPic.parent().width()},400);
            },6000);

        },
        wrapHover:function(){
            var _self=this;
            this.$wrap.hover(function(){
                clearInterval(_self.timer);
            },function(){
                _self.setTime();
            });
        }
    };

    new Teacher({
        bigPicWrap:$("#toggle_teacher"),
        smallPicWrap:$("#change_btn")
    });


    /*实战项目*/
    $(".A_Demo").PicCarousel("init");
    $(".B_Demo").PicCarousel({
        "width":1200,		//幻灯片的宽度
        "height":380,		//幻灯片的高度
        "posterWidth":660,	//幻灯片第一帧的宽度
        "posterHeight":360, //幻灯片第一张的高度
        "scale":0.9,		//记录显示比例关系
        "speed":300,		//记录幻灯片滚动速度
        "autoPlay":true,	//是否开启自动播放
        "delay":2000,		//自动播放间隔
        "verticalAlign":"bottom"	//图片对齐位置
    });

    /*返回顶部*/
    $(window).scroll(function(){
        var scrollTop=$(window).scrollTop();
        scrollTop>200?$(".back_top").fadeIn(200):$(".back_top").fadeOut(200);
    });
    $(".back_top").on("click",function(e){
        e.preventDefault();
        $("html,body").stop().animate({scrollTop:0},800,"easeInOutQuint");
    });

})(jQuery);
